from django.shortcuts import redirect, render, reverse, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.mixins import PermissionRequiredMixin, LoginRequiredMixin
from django.views.generic import ListView, DetailView, CreateView, DeleteView
from django.views.generic.base import View

from . import models


class PenaltyList(LoginRequiredMixin, PermissionRequiredMixin, ListView):
	permission_required = 'main.view_penalty'
	model = models.Penalty
	context_object_name = 'penalty'
	#template_name = 'main/penalty_list.html'


class PenaltyDetail(LoginRequiredMixin, PermissionRequiredMixin, DetailView):
	permission_required = 'main.view_penalty'
	model = models.Penalty
	context_object_name = 'penalty'


class PenaltyCreate(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
	permission_required = 'main.add_penalty'
	model = models.Penalty
	fields = ['user', 'reason', 'p_type']


class PenaltyDelete(LoginRequiredMixin, PermissionRequiredMixin, View):
	permission_required = 'main.delete_penalty'
	def get(self, request, pk):
		obj = get_object_or_404(models.Penalty, pk=pk)
		obj.delete()
		return redirect('/')


class PlayerCreate(LoginRequiredMixin, PermissionRequiredMixin, CreateView):
	permission_required = 'main.add_player'
	model = models.Player
	fields = ['user_name']